

#ifndef TFS_INTERNAL_DEFS_H__
#define TFS_INTERNAL_DEFS_H__

#include <stdint.h>
#include <stdbool.h>

// 
/*
#define CONFIG_START_ADDRESS		0
#define CONFIG_END_ADDRESS			4096 * 2 	// 8K bytes 

#define DATA_START_ADDRESS			4096 * 16 	// 
#define DATA_END_ADDRESS			4096 * 256 	// total 240 * 4 K  bytes data */
//================================================================================

#define TFS_OP_QUEUE_SIZE			8 
#define TFS_OP_QUEUE_SIZE_MASK		(TFS_OP_QUEUE_SIZE - 1) 

#define TFS_HEADER_SIZE				12 

#define KEY_ID_OFFSET				0
#define LENGTH_OFFSET				2 
#define FILE_ID_OFFSET				4
#define CRC_OFFSET					6 

#define RECORD_ID_OFFSET			8 

#define HEADER_KEY_LEN_SIZE         4
#define HEADER_FILE_CRC_SIZE        4
#define HEADER_RECORD_ID_SIZE       4 

#define TFS_ERASED_WORDS			0xFFFFFFFFUL

enum{
	TFS_FLAG_IDLE		= 0, 
	TFS_FLAG_EXECUTING = 0x01, 
};

typedef enum{
	TFS_OP_NONE, 
	
	TFS_OP_INIT, 
	TFS_OP_WRITE, 
	TFS_OP_UPDATE, 
	TFS_OP_DELETED_RECORD, 
	TFS_OP_ERASE, 
	
	TFS_OP_DONE, 
}tfs_op_code_t; 

typedef enum{
//	TFS_OP_WRITE_CHECK_ADDR, 	// this check used for check, if write address is the beginning of sector, erase it first 
//	TFS_OP_WRITE_CHUNKS_DATA, 	// write first, if overlap page, write remain later 
//	TFS_OP_WRITE_CHUNKS_DATA_COMPLETED, 
//	
//	TFS_OP_WRITE_CHUNKS_REMAIN, 
	
	TFS_OP_WRITE_FIND_RECORD, 	// find record id first 
    TFS_OP_WRITE_CHECK_BEGINING_ADDRESS,
    
	TFS_OP_WRITE_ERASE_SECTOR, 	// if update overlap sector, erase next sector for write operation 
//	TFS_OP_WRITE_ERASE_SECTOR_COMPLETED, 
		
	TFS_OP_WRITE_FLAG_DIRTY, 	// set last record key dirty 
//	TFS_OP_WRITE_FLAG_COMPETED, // flag a write operation is completed 
    
    TFS_OP_WRITE_BEGINNING,
    TFS_OP_WRITE_RECORD_ID,     // write record id 
    TFS_OP_WRITE_CHUNKS_DATA, 
    TFS_OP_WRITE_FINALIZE, 
	
	TFS_OP_WRITE_VERIFY, 		// verify is has corret data 
	TFS_OP_WRITE_DONE, 			// Flag a record as dirty (as part of an update operation).
}tfs_write_step_t; 

typedef enum{		
	TFS_OP_DELETED_FIND_RECORD, 
	TFS_OP_DELETED_HEADER, 
	TFS_OP_DELETED_FLAG_COMPETED, 
	
	TFS_OP_DELETED_VERITY, 	
	TFS_OP_DELETED_DONE, 
}tfs_del_step_t; 

typedef enum{
	TFS_OP_ERASE_CHUNKS, 
	
	TFS_OP_ERASE_FLAG_COMPLETED, 
	
	TFS_OP_ERASED_DONE, 
}tfs_erase_step_t; 

typedef struct{
    uint16_t 		record_key; 	
	uint16_t 		length_bytes; 
    
    uint16_t 		file_id; 
	uint16_t 		crc; 
	
	uint32_t 		record_id; 
}tfs_header_t; 


#if defined(__CC_ARM)
    #pragma push
    #pragma anon_unions
#elif defined(__ICCARM__)
    #pragma language=extended
#elif defined(__GNUC__)
    // anonymous unions are enabled by default
#endif

typedef struct{
	tfs_op_code_t	op_code; 
	
	union{
		struct{
			tfs_header_t		header; 
			tfs_write_step_t	step; 
			uint32_t 			addr; 					// write begin address 
			uint32_t 			offset; 			
			uint32_t 			record_to_deleted; 		// The record to delete in case this is an update.
		}write; 
		
		struct{
			tfs_del_step_t		step; 
			uint32_t 			record_id; 
			uint32_t 			addr; 
		}del; 
		
		struct{
			tfs_erase_step_t	step; 
			uint32_t 			addr; 					// erase a sector 
			uint32_t 			nums;					// number of sector need to erase 
			uint32_t 			offset; 
		}erase; 
	}; 
}tfs_op_t; 

#if defined(__CC_ARM)
    #pragma pop
#elif defined(__ICCARM__)
    // leave anonymous unions enabled
#elif defined(__GNUC__)
    // anonymous unions are enabled by default
#endif

typedef struct{
	tfs_op_t				op[ TFS_OP_QUEUE_SIZE ];
	uint32_t 				rp; 
	uint32_t 				count; 
}tfs_op_queue_t;


typedef struct{
	uint8_t *				wbp[ TFS_OP_QUEUE_SIZE ] ;
	uint32_t 				rp; 
	uint32_t 				count; 	
}tfs_chunks_queue_t; 




#endif 

